<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Area\AllArea;
use App\Admin\Actions\Area\ClearArea;
use App\Admin\Actions\Area\ProvinceArea;
use App\Miya\Models\Area;
use App\Miya\Models\ChinaArea;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Overtrue\Pinyin\Pinyin;

class AreaController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '地区管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Area());
        $grid->model()->with(['province', 'city', 'district']);

        $grid->column('id', '序号');
        $grid->column('name', '地区名称')->editable();
        $grid->column('tag', '地区标签')->editable();
        $grid->column('area_string', '地区层级')->display(function () {
            $area = [];
            $area[] = $this->province->name;
            $this->city && $area[] = $this->city->name;
            $this->district && $area[] = $this->district->name;

            return implode('-', $area);
        })->label();
        $grid->column('rank', '排序')->sortable()->editable()->help('从大到小排序,影响前端地区显示');

        $grid->tools(function (Grid\Tools $tools) {
            //添加生成全国地区的快捷方式
            $tools->append(new AllArea());
            $tools->append(new ClearArea());
        });
        $grid->filter(function (Grid\Filter $filter) {
            $filter->disableIdFilter();

            $filter->like('name', '地区');
        });
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Area());

        $form->text('name', '地区名')->help('不填写则为末尾下拉框的地区');
        $form->distpicker([
            'province_id' => '省',
            'city_id' => '市',
            'district_id' => '区'
        ], '地域选择')->default([
            'province' => 320000,
            'city' => 320500,
            'district' => 320583,
        ]);

        $form->hidden('tag');
        $form->saving(function (Form $form) {
            if ($form->province_id > 0) {
                $areas = ChinaArea::whereIn('code', [$form->province_id, $form->city_id, $form->district_id])
                    ->pluck('name');
                $pinyin = new Pinyin();
                $level = $areas->join("_");
                if ($form->name) {
                    $level .= '_' . $form->name;
                }
                $form->name = $form->name ?? array_reverse($areas->toArray())[0];
                $form->tag = sprintf('%s', $pinyin->abbr($level, PINYIN_KEEP_PUNCTUATION));
            }
        });
        $form->slider('rank', '地区排序')->default(1)->options([
            'max' => 100,
            'min' => 1,
            'step' => 1,
        ]);

        $form->divider();

        $form->text('title', '自定义标题');
        $form->textarea('description', '自定义描述');

        return $form;
    }
}
